package org.duanyu.shopping_manager_api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.val;
import org.apache.dubbo.config.annotation.DubboReference;
import org.duanyu.shopping_common.pojo.Admin;
import org.duanyu.shopping_common.result.BaseResult;
import org.duanyu.shopping_common.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员的控制资源
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @DubboReference
    private AdminService adminService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 新增管理员
     * @param admin
     * @return
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Admin admin) {
        String password = admin.getPassword();
        password = passwordEncoder.encode(password);
        admin.setPassword(password);
        adminService.AddAdmin(admin);
        return BaseResult.success();
    }

    /**
     * 修改管理员
     * @param admin
     * @return
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Admin admin) {
        String password = admin.getPassword();
        if (StringUtils.hasText(password)) {
            //密码不为空才能进行加密
            password=passwordEncoder.encode(password);
        }
        admin.setPassword(password);
        adminService.UpdateAdmin(admin);
        return BaseResult.success();
    }


    /**
     * 删除管理员
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long id) {
        adminService.deleteAdmin(id);
        return BaseResult.success();
    }

    /**
     * 根据id查询管理员
     * @param aid
     * @return
     */
    @GetMapping("/findById")
    public BaseResult<Admin> findById(Long aid) {
        Admin admin = adminService.findById(aid);
        return BaseResult.success(admin);
    }

    /**
     * 分页查询管理员
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search")
    @PreAuthorize("hasAnyAuthority('/admin/search')")
    public BaseResult<Page<Admin>> search(int page,int size) {
        Page<Admin> adminPage = adminService.findAllAdmin(page, size);
        return BaseResult.success(adminPage);
    }

    /**
     * 添加管理员角色
     * @param aid
     * @param rids
     * @return
     */
    @PutMapping("/updateRoleToAdmin")
    public BaseResult updateRoleToAdmin(Long aid,Long[] rids){
        adminService.updateAdminRole(aid,rids);
        return BaseResult.success();
    }

    /**
     * 获取登录的管理员名
     * @return 管理员名
     */
    @GetMapping("/getUsername")
    public BaseResult<String> getUsername() {
        //1.获取绘画对象
        SecurityContext context = SecurityContextHolder.getContext();
        //2.获取认证对象
        Authentication authentication = context.getAuthentication();
        //3.获取登录用户信息
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        return BaseResult.success(userDetails.getUsername());
    }

}
